package com.isoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.entity.FlightRouteChangeHistory;
import com.isoft.result.R;
import com.isoft.service.FlightRouteChangeHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/flightRouteChangeHistory")
public class FlightRouteChangeHistoryController {

    @Autowired
    private FlightRouteChangeHistoryService flightRouteChangeHistoryService;

    // 查询单条历史记录
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        FlightRouteChangeHistory history = flightRouteChangeHistoryService.getById(id);
        return history != null ? R.ok(history) : R.error("未找到数据");
    }

    // 分页查询历史记录
    @GetMapping("/list")
    public R list(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String flightNumber,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String applicantName,
            @RequestParam(required = false) String approverName
    ) {
        Page<FlightRouteChangeHistory> page = new Page<>(pageNo, pageSize);
        QueryWrapper<FlightRouteChangeHistory> queryWrapper = new QueryWrapper<>();

        if (flightNumber != null && !flightNumber.isEmpty()) {
            queryWrapper.like("FLIGHT_NUMBER", flightNumber);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq("STATUS", status);
        }
//        if (applicantName != null && !applicantName.isEmpty()) {
//            queryWrapper.like("APPLICANT_NAME", applicantName);
//        }
//        if (approverName != null && !approverName.isEmpty()) {
//            queryWrapper.like("APPROVER_NAME", approverName);
//        }

        queryWrapper.orderByDesc("CREATE_TIME");
        Page<FlightRouteChangeHistory> result = flightRouteChangeHistoryService.page(page, queryWrapper);
        return R.ok(result);
    }
}